package com.xinzhi.service.impl;

import com.xinzhi.domain.Supplier;
import com.xinzhi.mapper.SupplierMapper;
import com.xinzhi.service.SupplierService;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    SupplierMapper supplierMapper;

    @Override
    public Result updateSupplier(Supplier supplier) {
        //进行判断
        if (supplier != null) {
            //判断不为空的话，动态sql修改
            int i = supplierMapper.updateById(supplier);
            //返回受影响行数，进行判断。大于零的话返回给前端修改成功
            if (i > 0) {
                return Result.ok("修改成功");
            } else
                return Result.error("修改失败");
        }
        return Result.error("不能为空");
    }

    @Override
    public List<Supplier> selectList() {
        //查询表中全部的信息
        List<Supplier> suppliers = supplierMapper.selectList(null);
        //将查询出来的全部信息返会给前端
        return suppliers;
    }

    @Override
    public Result delete(Supplier supplier) {
        if (supplier != null) {
            //通过id进行删除
            int i = supplierMapper.updateById(supplier);
            if (i > 0) {
                return Result.ok("删除成功");
            }
            //返回前端一个状态
            return Result.error("删除失败");
        }
        return Result.error("不能为空");
    }

    @Override
    public Result insert(Supplier supplier) {
        //新增 给具体的对象赋值
        int insert = supplierMapper.inSupplier(supplier);

        if(insert>0){
            return Result.ok("新增成功");
        }
        return Result.error(500,"新增失败");
    }
}
